/*
 * @Description: 页面路由 - 懒加载
 * @Author: 0001100986/LiuShuai
 * @Date: 2021-06-22 14:09:49
 * @LastEditTime: 2021-11-02 09:32:40
 * @LastEditors: 0001100986/LiuShuai
 */
import Vue from 'vue';
import store from '@/store';
import Router from 'vue-router';
import Home from '@/pages/home';
import Console from '@/pages/dashboard';
import setting from '@/config/setting';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

NProgress.configure({ showSpinner: false, easing: 'ease', speed: 500 });
Vue.use(Router);

// 不需要权限的路由
export const routes = [
  {
    path: '/',
    redirect: '/dashboard/console'
  }, {
    path: '/dashboard',
    component: Home,
    meta: {
      title: 'IONE'
    },
    children: [
      {
        path: 'console',
        component: Console,
        meta: {
          title: '工作台'
        }
      }
    ]
  }, {
    path: '/login',
    component: () => import('@/pages/login'),
    meta: {
      title: '登录',
      newPage: true
    }
  }, {
    path: '/exception',
    component: Home,
    meta: {
      title: '异常页面'
    },
    children: [
      {
        path: '403',
        component: () => import('@/pages/exception/403'),
        meta: {
          title: '403'
        }
      },
      {
        path: '404',
        component: () => import('@/pages/exception/404'),
        meta: {
          title: '404'
        }
      },
      {
        path: '500',
        component: () => import('@/pages/exception/500'),
        meta: {
          title: '500'
        }
      }
    ]
  }
];

const router = new Router({
  mode: 'hash',
  routes
});

// 需要权限的路由
export const allowRouters = [
  {
    path: '/monitor',
    component: Home,
    meta: {
      title: '网络监控'
    },
    children: [
      {
        path: 'ne-monitor',
        component: () => import('@/pages/monitor/ne'),
        meta: {
          title: '网元监控'
        }
      },
      {
        path: 'map-monitor',
        component: () => import('@/pages/monitor/map'),
        meta: {
          title: '地图模式'
        }
      },
      {
        path: 'device-info',
        component: () => import('@/pages/monitor/device'),
        meta: {
          title: '设备信息'
        }
      },
      {
        path: 'ne-report',
        component: () => import('@/pages/monitor/report'),
        meta: {
          title: '网元报表'
        }
      },
      {
        path: 'topology',
        component: () => import('@/pages/monitor/topology'),
        meta: {
          title: '拓扑监控'
        }
      }
    ]
  }, {
    path: '/network',
    component: Home,
    meta: {
      title: '网络编排'
    },
    children: [
      {
        path: 'ne-manage',
        component: () => import('@/pages/network/ne'),
        meta: {
          title: '网元管理'
        }
      },
      {
        path: 'site-manage',
        component: () => import('@/pages/network/site'),
        meta: {
          title: '站点管理'
        }
      },
      {
        path: 'backbone-manage',
        component: () => import('@/pages/network/backbone'),
        meta: {
          title: '骨干网络编排'
        }
      },
      {
        path: 'tunnel-manage',
        component: () => import('@/pages/network/tunnel'),
        meta: {
          title: '隧道编排'
        }
      },
      {
        path: 'overlay-manage',
        component: () => import('@/pages/network/overlay'),
        meta: {
          title: 'Overlay编排'
        }
      },
      {
        path: 'vpn-manage',
        component: () => import('@/pages/network/vpn'),
        meta: {
          title: 'VPN管理'
        }
      }
    ]
  }, {
    path: '/configure',
    component: Home,
    meta: {
      title: '网元配置'
    },
    children: [
      {
        path: 'access',
        component: () => import('@/pages/configure/access'),
        meta: {
          title: '接入配置'
        }
      },
      {
        path: 'mixed',
        component: () => import('@/pages/configure/mixed'),
        meta: {
          title: '汇聚配置'
        }
      }
    ]
  }, {
    path: '/software',
    component: Home,
    meta: {
      title: '软终端管理'
    },
    children: [
      {
        path: 'introduce',
        component: () => import('@/pages/software/introduce'),
        meta: {
          title: '软终端配置'
        }
      },
      {
        path: 'journal',
        component: () => import('@/pages/software/journal'),
        meta: {
          title: '日志记录'
        }
      },
      {
        path: 'parameter',
        component: () => import('@/pages/software/parameter'),
        meta: {
          title: '参数配置'
        }
      }
    ]
  }, {
    path: '/alarms',
    component: Home,
    meta: {
      title: '运维管理'
    },
    children: [
      {
        path: 'activity',
        component: () => import('@/pages/alarms/activity'),
        meta: {
          title: '活动告警'
        }
      },
      {
        path: 'history',
        component: () => import('@/pages/alarms/history'),
        meta: {
          title: '历史告警'
        }
      },
      {
        path: 'rule',
        component: () => import('@/pages/alarms/rule'),
        meta: {
          title: '告警规则'
        }
      },
    ]
  }, {
    path: '/user',
    component: Home,
    meta: {
      title: '用户管理'
    },
    children: [
      {
        path: 'personal-settings',
        component: () => import('@/pages/user/personal'),
        meta: {
          title: '个人中心'
        }
      },
      {
        path: 'account-manage',
        component: () => import('@/pages/user/account'),
        meta: {
          title: '账户管理'
        }
      },
      {
        path: 'tenant-manage',
        component: () => import('@/pages/user/tenant'),
        meta: {
          title: '租户管理'
        }
      }
      // {
      //   path: 'operations',
      //   component: () => import('@/pages/user/operations'),
      //   meta: {
      //     title: '历史记录'
      //   }
      // },
      // {
      //   path: 'account-rule',
      //   component: () => import('@/pages/user/rule'),
      //   meta: {
      //     title: '账户规则'
      //   }
      // }
    ]
  }, {
    path: '/system',
    component: Home,
    meta: {
      title: '系统设置'
    },
    children: [
      {
        path: 'email-server',
        component: () => import('@/pages/system/email'),
        meta: {
          title: '邮件服务器'
        }
      },
      {
        path: 'ne-backups',
        component: () => import('@/pages/system/backups'),
        meta: {
          title: '网元备份'
        }
      },
      {
        path: 'database-backups',
        component: () => import('@/pages/system/databack'),
        meta: {
          title: '数据库备份'
        }
      },
      {
        path: 'database-recovery',
        component: () => import('@/pages/system/datarecovery'),
        meta: {
          title: '数据库恢复'
        }
      },
      {
        path: 'parameter',
        component: () => import('@/pages/system/parameter'),
        meta: {
          title: '系统参数'
        }
      },
      {
        path: 'logs',
        component: () => import('@/pages/system/journal'),
        meta: {
          title: '系统日志'
        }
      },
      {
        path: 'about-ione',
        component: () => import('@/pages/system/about'),
        meta: {
          title: '关于IONE'
        }
      }
    ]
  }
];

export const allRoutes = routes.concat(allowRouters);

router.beforeEach((to, from, next) => {
  let isLogin = false;
  let { meta, matched } = to;
  let { title, title_en, newPage, keepAlive } = meta;
  let { showNprogress } = store.state.setting.setting;
  let sys = JSON.parse(localStorage.getItem('sys'));

  if (showNprogress) {
    NProgress.start();
  }

  if (sys) {
    isLogin = sys.user.isLogin;
  }

  to.params.keepAlive = keepAlive;

  if (!isLogin && to.path !== '/login') {
    next('/login');
    return;
  } else {
    let { menuList } = store.state.menu;
    // 菜单数据加载成功
    if (menuList.length > 0) {
      // 打开的页面不存在
      if (!matched.length) {
        router.push('/exception/404');
        return;
      }
    }
    next();
  }

  // 不是标签页
  if (newPage) {
    return;
  }

  // 路由添加到标签页
  store.dispatch('worktab/worktabRoute', {
    to: {
      name: to.name ? to.name : '',
      title: (to.meta && title) ? title : '',
      title_en: (to.meta && title_en) ? title_en : '',
      path: to.path,
      params: to.params
    },
    from: {
      name: from.name ? from.name : '',
      title: (from.meta && from.meta.title) ? from.meta.title : '',
      title_en: (from.meta && from.meta.title_en) ? from.meta.title_en : '',
      path: from.path,
      params: to.params
    }
  });

  // 设置网页title
  if (title) {
    document.title = `${title} - ${setting.systemName}`;
  }
  return;
});

router.afterEach(() => {
  let { showNprogress } = store.state.setting.setting;

  if (showNprogress) {
    NProgress.done();
  }
});

export default router;